﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
namespace ToolsLayer
{
    public class ViewManager
    {
        /// <summary>
        /// 根据xml配置文件动态生成GridView的列
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static DataControlFieldCollection GetBoundColumns(string filename)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(filename);
            string xpath = "list/*";
            XmlNodeList nodes = xmlDoc.SelectNodes(xpath);
            if (nodes == null || nodes.Count == 0)
                return null;
            DataControlFieldCollection rtn = new DataControlFieldCollection();
            foreach (XmlNode node in nodes)
            {
                XmlElement xe = (XmlElement)node;
                if (xe.GetAttribute("show").ToLower() == "false")
                    continue;
                BoundField bf = new BoundField();
                bf.DataField = xe.GetAttribute("columnname");
                bf.HeaderText = xe.GetAttribute("name");
                bf.ItemStyle.CssClass = xe.GetAttribute("css"); 
                bf.HeaderStyle.CssClass = xe.GetAttribute("css"); 

                /*bf.HeaderStyle.BorderWidth = Unit.Pixel(1);
                bf.ItemStyle.BorderWidth = Unit.Pixel(1);
                bf.ItemStyle.Width = Unit.Parse(xe.GetAttribute("width"));
                 * */
                rtn.Add(bf);
            }
            return rtn;
        }

        public static void BindGridView(GridView gv, string filename)
        {
            gv.Columns.Clear();
            DataControlFieldCollection columns =
                ViewManager.GetBoundColumns(filename);
            for (int i = 0; i < columns.Count; i++)
            {
                gv.Columns.Add(columns[i]);
            }
        }

        //public static void SetTabstripStyle(Microsoft.Web.UI.WebControls.TabStrip t)
        //{

        //    t.SepDefaultStyle = Microsoft.Web.UI.WebControls.CssCollection.FromString("border-bottom:solid 1px #5d61ab;");
        //    t.TabDefaultStyle=Microsoft.Web.UI.WebControls.CssCollection.FromString("border: solid 1px #006699; background: #5d61ab; padding-top: 3px; padding-bottom: 2px; padding-left: 5px; padding-right: 5px; text-align: center; color: #ffffff;");
        //    t.TabHoverStyle=Microsoft.Web.UI.WebControls.CssCollection.FromString("color:red;") ;
        //    t.TabSelectedStyle = Microsoft.Web.UI.WebControls.CssCollection.FromString("border: solid 1px #5d61ab;border-bottom: none;  padding-left: 5px; padding-right: 5px; color: black;");
        //}
    }
}
